///<reference path='SPRESTListCollection.ts'/>

// Module
module SPREST {

    // Class
    export class Web {

        public APIUrl: string = null;
        public urlFirstUniqueAncestorSecurableObject: string = null;
        public urlRoleAssignments: string = null;
        public urlAllProperties: string = null;
        public urlAssociatedMemberGroup: string = null;
        public urlAssociatedOwnerGroup: string = null;
        public urlAssociatedVisitorGroup: string = null;
        public urlAvailableContentTypes: string = null;
        public urlAvailableFields: string = null;
        public urlContentTypes: string = null;
        public urlCurrentUser: string = null;
        public urlEventReceivers: string = null;
        public urlFeatures: string = null;
        public urlFields: string = null;
        public urlFolders: string = null;
        public urlLists: string = null;
        public urlListTemplates: string = null;
        public urlNavigation: string = null;
        public urlParentWeb: string = null;
        public urlPushNotificationSubscribers: string = null;
        public urlRecycleBin: string = null;
        public urlRegionalSettings: string = null;
        public urlRoleDefinitions: string = null;
        public urlRootFolder: string = null;
        public urlSiteGroups: string = null;
        public urlSiteUserInfoList: string = null;
        public urlSiteUsers: string = null;
        public urlThemeInfo: string = null;
        public urlUserCustomActions: string = null;
        public urlWebs: string = null;
        public urlWebInfos: string = null;
        public urlWorkflowAssociations: string = null;
        public urlWorkflowTemplates: string = null;
        public allowRssFeeds: bool = null;
        public appInstanceId: string = null;
        public configuration: number = null;
        public created: string = null;
        public customMasterUrl: string = null;
        public description: string = null;
        public documentLibraryCalloutOfficeWebAppPreviewersDisabled: bool = null;
        public enableMinimalDownload: bool = null;
        public id: string = null;
        public language: number = null;
        public lastItemModifiedDate: string = null;
        public masterUrl: string = null;
        public quickLaunchEnabled: bool = null;
        public recycleBinEnabled: bool = null;
        public serverRelativeUrl: string = null;
        public syndicationEnabled: bool = null;
        public title: string = null;
        public treeViewEnabled: bool = null;
        public uIVersion: number = null;
        public uIVersionConfigurationEnabled = null;
        public reportedUrl: string = null;
        public webTemplate: string = null;
        
        public allLists: SPREST.ListCollection = null;
        public started: bool = false;
        public ready: bool = false;
        public dfd: JQueryDeferred = $.Deferred();

        // Constructor
        constructor (public Context: SPREST.Context, public Site: SPREST.Site, public Url: string = null) {
                
         }

        GetLists(): SPREST.ListCollection {
            if (this.allLists != null) return this.allLists;

            var lc: SPREST.ListCollection = new SPREST.ListCollection(this.Context, this);
            this.allLists = lc;
            var dfd: JQueryDeferred = lc.start();
            return lc;
        }

        start(): JQueryDeferred {
            var myThis: Web = this;
            if (myThis.started) return (myThis.dfd);
            myThis.started = true;

            myThis.APIUrl = Util.concatenateUrl(this.Context.contextWebUrl, SPREST.Constants.WebApiUrl);
            $.ajax(myThis.APIUrl, {
                async: true,
                headers: { "Accept":"application/json;odata=verbose" },
                type: "GET"
            }).fail(function(req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.urlFirstUniqueAncestorSecurableObject = data.d.FirstUniqueAncestorSecurableObject.__deferred.uri;
                myThis.urlRoleAssignments = data.d.RoleAssignments.__deferred.uri;
                myThis.urlAllProperties = data.d.AllProperties.__deferred.uri;
                myThis.urlAssociatedMemberGroup = data.d.AssociatedMemberGroup.__deferred.uri;
                myThis.urlAssociatedOwnerGroup = data.d.AssociatedOwnerGroup.__deferred.uri;
                myThis.urlAssociatedVisitorGroup = data.d.AssociatedVisitorGroup.__deferred.uri;
                myThis.urlAvailableContentTypes = data.d.AvailableContentTypes.__deferred.uri;
                myThis.urlAvailableFields = data.d.AvailableFields.__deferred.uri;
                myThis.urlContentTypes = data.d.ContentTypes.__deferred.uri;
                myThis.urlCurrentUser = data.d.CurrentUser.__deferred.uri;
                myThis.urlEventReceivers = data.d.EventReceivers.__deferred.uri;
                myThis.urlFeatures = data.d.Features.__deferred.uri;
                myThis.urlFields = data.d.Fields.__deferred.uri;
                myThis.urlFolders = data.d.Folders.__deferred.uri;
                myThis.urlLists = data.d.Lists.__deferred.uri;
                myThis.urlListTemplates = data.d.ListTemplates.__deferred.uri;
                myThis.urlNavigation = data.d.Navigation.__deferred.uri;
                myThis.urlParentWeb = data.d.ParentWeb.__deferred.uri;
                myThis.urlPushNotificationSubscribers = data.d.PushNotificationSubscribers.__deferred.uri;
                myThis.urlRecycleBin = data.d.RecycleBin.__deferred.uri;
                myThis.urlRegionalSettings = data.d.RegionalSettings.__deferred.uri;
                myThis.urlRoleDefinitions = data.d.RoleDefinitions.__deferred.uri;
                myThis.urlRootFolder = data.d.RootFolder.__deferred.uri;
                myThis.urlSiteGroups = data.d.SiteGroups.__deferred.uri;
                myThis.urlSiteUserInfoList = data.d.SiteUserInfoList.__deferred.uri;
                myThis.urlSiteUsers = data.d.SiteUsers.__deferred.uri;
                myThis.urlThemeInfo = data.d.ThemeInfo.__deferred.uri;
                myThis.urlUserCustomActions = data.d.UserCustomActions.__deferred.uri;
                myThis.urlWebs = data.d.Webs.__deferred.uri;
                myThis.urlWebInfos = data.d.WebInfos.__deferred.uri;
                myThis.urlWorkflowAssociations = data.d.WorkflowAssociations.__deferred.uri;
                myThis.urlWorkflowTemplates = data.d.WorkflowTemplates.__deferred.uri;
                myThis.allowRssFeeds = data.d.AllowRssFeeds;
                myThis.appInstanceId = data.d.AppInstanceId;
                myThis.configuration = data.d.Configuration;
                myThis.created = data.d.Created;
                myThis.customMasterUrl = data.d.CustomMasterUrl;
                myThis.description = data.d.Description;
                myThis.documentLibraryCalloutOfficeWebAppPreviewersDisabled = data.d.DocumentLibraryCalloutOfficeWebAppPreviewersDisabled;
                myThis.enableMinimalDownload = data.d.EnableMinimalDownload;
                myThis.id = data.d.Id;
                myThis.language = data.d.Language;
                myThis.lastItemModifiedDate = data.d.LastItemModifiedDate;
                myThis.masterUrl = data.d.MasterUrl;
                myThis.quickLaunchEnabled = data.d.QuickLaunchEnabled;
                myThis.recycleBinEnabled = data.d.RecycleBinEnabled;
                myThis.serverRelativeUrl = data.d.ServerRelativeUrl;
                myThis.syndicationEnabled = data.d.SyndicationEnabled;
                myThis.title = data.d.Title;
                myThis.treeViewEnabled = data.d.TreeViewEnabled;
                myThis.uIVersion = data.d.UIVersion;
                myThis.uIVersionConfigurationEnabled = data.d.UIVersionConfigurationEnabled;
                myThis.reportedUrl = data.d.Url;
                myThis.webTemplate = data.d.WebTemplate;

                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        }
        GetFolderByServerRelativeUrl(url: string): SPREST.Folder {
            var APIUrl: string = SPREST.Util.concatenateUrl(this.APIUrl, "GetFolderByServerRelativeUrl('" + url + "')");
            var folder = new SPREST.Folder(this.Context, this, APIUrl);
            folder.start();
            return folder;
        }
    }

}
